home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / alv.sun / alv.lha / src / winlev8.c < prev   
Encoding:
C/C++ Source or Header  |  1992-11-08  |  7.4 KB  |  337 lines

  1. #include <suntool/sunview.h>
  2. #include <suntool/canvas.h>
  3. #include <suntool/panel.h>
  4. #include <stdio.h>
  5. #include "defs.h"
  6.  
  7. #define WINLEVFONT "/usr/lib/fonts/fixedwidthfonts/cour.r.16"
  8. #define XSIZE 256
  9. #define YSIZE 256
  10.  
  11. har           *progname;
  12.  
  13. tatic short    winicon[] =
  14. {
  15. #include "../images/icons/winlev8.icon"
  16. };
  17.  
  18. EFINE_ICON_FROM_IMAGE(icon, winicon);
  19.  
  20. nt             boxsize = 2;
  21. tatic void     getcoords(), repaint_canvas(), invert_proc(), colour_proc(), quit_proc(), reset_proc();
  22. nt             invert = 0;
  23. nt             colour = 0;
  24. nt             base = 20;
  25. nt             window = 255, level = 127;
  26. har           *progname;
  27.  
  28. Frame           frame;
  29. anvas          canvas, canvas2;
  30. anel           panel, inv_panel, col_panel;
  31. ixfont        *font;
  32. ixwin         *pw, *pw2;
  33. har            cmsname[CMS_NAMESIZE];
  34. _char          red[256], green[256], blue[256];
  35.  
  36. #ifdef STANDALONE
  37. ain(argc, argv, envp)
  38. #else
  39. inlev8_main(argc, argv, envp)
  40. #endif
  41.     int             argc;
  42.     char          **argv;
  43.     char          **envp;
  44.  
  45. {
  46.     int             i, j, x, y;
  47.     u_char         *pi;
  48.     char           *makelabel();
  49.  
  50.     progname = strsave(argv[0]);
  51.     parse_profile(&argc, argv, envp);
  52.  
  53.     while ((gc = getopt(argc, argv, " ")) != EOF)
  54.         switch (gc) {
  55.         case '?':
  56.             errflag++;
  57.             break;
  58.         }
  59.  
  60.     if (errflag)
  61.         error((char *) 0, "Usage: %s:\n", progname);
  62.  
  63.     if ((font = pf_open(WINLEVFONT)) == NULL) {
  64.         fprintf(stderr, "%s : Cannot open %s\n", argv[0], WINLEVFONT);
  65.         exit(1);
  66.     }
  67.     boxsize = 1;
  68.  
  69.     /* Initialise variables used to set colour map segment */
  70.     for (i = 0; i < 256; i++) {
  71.         red[i] = i;
  72.         green[i] = i;
  73.         blue[i] = i;
  74.     }
  75.     if (mono_override) {
  76.         red[0] = blue[0] = green[0] = 255;
  77.         red[254] = blue[254] = green[254] = 255;
  78.         red[255] = blue[255] = green[255] = 0;
  79.         }
  80.  
  81.     /* create frame and canvas */
  82.     frame = window_create(NULL, FRAME,
  83.                   FRAME_LABEL, makelabel(),
  84.                   WIN_HEIGHT, YSIZE * boxsize + 78 + base,
  85.                   WIN_WIDTH, XSIZE * boxsize + 10,
  86.                   FRAME_ICON, &icon,
  87.                   FRAME_ARGS, argc, argv,
  88.                   0);
  89.  
  90.     panel = window_create(frame, PANEL, 0);
  91.  
  92.     inv_panel = panel_create_item(panel, PANEL_CYCLE,
  93.                    PANEL_CHOICE_STRINGS, "Normal", "Inv", 0,
  94.                       PANEL_NOTIFY_PROC, invert_proc,
  95.                       0);
  96.     col_panel = panel_create_item(panel, PANEL_CYCLE,
  97.                       PANEL_CHOICE_STRINGS, "Grey", "Col", 0,
  98.                       PANEL_NOTIFY_PROC, colour_proc,
  99.                       0);
  100.     panel_create_item(panel, PANEL_BUTTON,
  101.         PANEL_LABEL_IMAGE, panel_button_image(panel, "Reset", 0, 0),
  102.               PANEL_NOTIFY_PROC, reset_proc,
  103.               0);
  104.     panel_create_item(panel, PANEL_BUTTON,
  105.          PANEL_LABEL_IMAGE, panel_button_image(panel, "Quit", 0, 0),
  106.               PANEL_NOTIFY_PROC, quit_proc,
  107.               0);
  108.  
  109.     window_fit(panel);
  110.     window_set(panel, WIN_WIDTH, XSIZE, 0);
  111.  
  112.     canvas2 = window_create(frame, CANVAS,
  113.                 WIN_BELOW, panel,
  114.                 WIN_X, 0,
  115.                 WIN_WIDTH, XSIZE,
  116.                 WIN_HEIGHT, 10,
  117.                 0);
  118.     pw2 = canvas_pixwin(canvas2);
  119.  
  120.     /* Set colour map segment */
  121.     pw_setcmsname(pw2, "greyscale8");
  122.     pw_putcolormap(pw2, 0, 256, red, green, blue);
  123.     draw_ramp();
  124.  
  125.     canvas = window_create(frame, CANVAS,
  126.                    WIN_BELOW, canvas2,
  127.                    WIN_X, 0,
  128.                    WIN_EVENT_PROC, getcoords,
  129.                    0);
  130.  
  131.     /* get the canvas pixwin to draw into */
  132.     pw = canvas_pixwin(canvas);
  133.  
  134.     /* Set colour map segment */
  135.     pw_setcmsname(pw, "greyscale8");
  136.     pw_putcolormap(pw, 0, 256, red, green, blue);
  137.  
  138.     window_set(canvas, WIN_CONSUME_PICK_EVENTS, LOC_DRAG, 0, 0);
  139.  
  140.     pw_vector(pw, 0, 255, 255, 255, PIX_SRC, 255);
  141.     pw_vector(pw, 127, 255, 127, 0, PIX_SRC, 255);
  142.     pw_vector(pw, 0, 255, 255, 0, PIX_SRC, 255);
  143.     pw_vector(pw, 255, 255, 0, 0, PIX_SRC, 255);
  144.     pr_text(pw->pw_prretained, 0, 128, PIX_SRC, font, "Window");
  145.     pr_text(pw->pw_prretained, 108, 250, PIX_SRC, font, "Level");
  146.  
  147.     window_main_loop(frame);
  148. }
  149.  
  150. tatic void
  151. nvert_proc(item, value, event)
  152.     Panel_item      item;
  153.     int             value;
  154.     Event          *event;
  155. {
  156.     invert = value;
  157.     setupcolourmap();
  158. }
  159.  
  160. tatic void
  161. olour_proc(item, value, event)
  162.     Panel_item      item;
  163.     int             value;
  164.     Event          *event;
  165. {
  166.     colour = value;
  167.     setupcolourmap();
  168. }
  169.  
  170.  
  171. tatic void
  172. etcoords(canvas, event, arg)
  173.     Canvas          canvas;
  174.     Event          *event;
  175.     caddr_t         arg;
  176. {
  177.     char           *makelabel();
  178.  
  179.     if ((event_id(event) == LOC_DRAG)
  180.         || (event_id(event) == MS_LEFT)
  181.         || (event_id(event) == MS_RIGHT)
  182.         || (event_id(event) == MS_MIDDLE)) {
  183.         window = (255 - event_y(event)) * 2;    /* allows double window
  184.                              * if desired */
  185.         window = (window < 0) ? 0 : window;
  186.         level = event_x(event);
  187.  
  188.         setupcolourmap();
  189.         window_set(frame, FRAME_LABEL, makelabel(), 0);
  190.     }
  191. }
  192.  
  193. etupcolourmap()
  194. {
  195.     int             col, i;
  196.     double          m;
  197.  
  198.     /* Initialise variables used to set colour map segment */
  199.     for (i = 0; i < 256; i++) {
  200.         if (window) {
  201.             m = 256.0 / window;
  202.             col = m * (i - level) + 127;
  203.         } else
  204.             col = (i < level) ? 0 : 255;
  205.         col = (col < 0) ? 0 :
  206.             (col > 255) ? 255 : col;
  207.         col = (invert) ? 255 - col : col;
  208.         if (colour) {
  209.             getrgb(col, &red[i], &green[i], &blue[i]);
  210.         } else {
  211.             red[i] = col;
  212.             green[i] = col;
  213.             blue[i] = col;
  214.         }
  215.     }
  216.     if (mono_override) {
  217.         red[0] = green[0] = blue[0] = 255;
  218.         /*red[254] = blue[254] = green[254] = 255;*/
  219.         red[255] = blue[255] = green[255] = 0;
  220.         }
  221.     pw_putcolormap(pw, 0, 256, red, green, blue);
  222.     pw_putcolormap(pw2, 0, 256, red, green, blue);
  223. }
  224.  
  225. har           *
  226. akelabel()
  227. {
  228.     static char     buf[BUFSIZ];
  229.     char           *sprintf();
  230.  
  231.     return sprintf(buf, "Window = %3d  Level = %3d", window, level);
  232. }
  233.  
  234. etrgb(val, r, g, b)
  235.     int             val;
  236.     u_char         *r, *g, *b;
  237. {
  238.     static float    coltab[48][3] = {
  239.         1.0, 1.0, 1.0,
  240.         1.0, 0.75, 0.75,
  241.         1.0, 0.5, 0.5,
  242.         1.0, 0.25, 0.25,
  243.         1.0, 0.0, 0.0,
  244.         /* 1.0, 0.064, 0.0, */
  245.         1.0, 0.193, 0.0,
  246.         1.0, 0.258, 0.0,
  247.         1.0, 0.323, 0.0,
  248.         1.0, 0.387, 0.0,
  249.         1.0, 0.452, 0.0,
  250.         1.0, 0.516, 0.0,
  251.         1.0, 0.548, 0.0,
  252.         1.0, 0.613, 0.0,
  253.         1.0, 0.677, 0.0,
  254.         1.0, 0.742, 0.0,
  255.         1.0, 0.806, 0.0,
  256.         1.0, 0.871, 0.0,
  257.         1.0, 0.936, 0.0,
  258.         1.0, 1.0, 0.0,
  259.         0.903, 1.0, 0.0,
  260.         0.806, 1.0, 0.0,
  261.         0.677, 1.0, 0.0,
  262.         0.516, 1.0, 0.0,
  263.         0.0, 1.0, 0.452,
  264.         0.0, 1.0, 0.710,
  265.         0.0, 1.0, 1.0,
  266.         0.0, 0.871, 1.0,
  267.         0.0, 0.677, 1.0,
  268.         0.0, 0.613, 1.0,
  269.         0.0, 0.548, 1.0,
  270.         0.0, 0.484, 1.0,
  271.         0.0, 0.419, 1.0,
  272.         0.0, 0.355, 1.0,
  273.         0.0, 0.290, 1.0,
  274.         0.0, 0.226, 1.0,
  275.         0.0, 0.161, 1.0,
  276.         0.0, 0.0, 1.0,
  277.         0.516, 0.0, 1.0,
  278.         0.581, 0.0, 1.0,
  279.         0.645, 0.0, 1.0,
  280.         0.710, 0.0, 1.0,
  281.         0.774, 0.0, 1.0,
  282.         0.839, 0.0, 1.0,
  283.         0.903, 0.0, 1.0,
  284.         0.968, 0.0, 1.0,
  285.         1.0, 0.0, 0.935,
  286.         1.0, 0.0, 0.839,
  287.         1.0, 0.0, 0.742
  288.  
  289.         /*
  290.          * 1.0, 0.0, 0.613, 1.0, 0.0, 0.484, 1.0, 0.0, 0.097
  291.          */
  292.     };
  293.  
  294.     int             val48;
  295.     float           rval48, roffset;
  296.     float           rr, rg, rb;
  297.  
  298.     rval48 = val / 5.426;
  299.     val48 = rval48;
  300.     roffset = rval48 - (float) val48;
  301.  
  302.     rr = (coltab[val48][0] * (1.0 - roffset) + coltab[val48 + 1][0] * roffset);
  303.     rg = (coltab[val48][1] * (1.0 - roffset) + coltab[val48 + 1][1] * roffset);
  304.     rb = (coltab[val48][2] * (1.0 - roffset) + coltab[val48 + 1][2] * roffset);
  305.     *r = ((rr * 0.9 + 0.1) * 255.0 + 0.5);
  306.     *g = ((rg * 0.85 + 0.15) * 255.0 + 0.5);
  307.     *b = ((rb * 0.9 + 0.1) * 255.0 + 0.5);
  308. }
  309.  
  310. tatic void
  311. uit_proc()
  312. {
  313.     window_set(frame, FRAME_NO_CONFIRM, TRUE, 0);
  314.     window_destroy(frame);
  315. }
  316.  
  317. tatic void
  318. eset_proc()
  319. {
  320.     window = 255;
  321.     level = 127;
  322.     invert = 0;
  323.     colour = 0;
  324.     panel_set_value(inv_panel, invert);
  325.     panel_set_value(col_panel, colour);
  326.     setupcolourmap();
  327.     window_set(frame, FRAME_LABEL, makelabel(), 0);
  328. }
  329.  
  330. raw_ramp()
  331. {
  332.     register        i;
  333.  
  334.     for (i = 0; i < XSIZE; i++)
  335.         pw_vector(pw2, i, 0, i, 10, PIX_SRC, i);
  336. }
  337.